//用于表单验证规则集合

// 验证手机号码
export function checkPhoneNumber(rule, value, callback) {
    const reg = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/;
    if (!value) {
        return callback(new Error('手机号码不能为空'))
    } else if (!reg.test(value)) {
        return callback(new Error('请填写正确的手机号码'))
    } else {
        callback()
    }
}

export function checkIdNum(rule, value, callback) {
    const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    if (!value) {
        return callback(new Error('证件号码不能为空'))
    } else if (!reg.test(value)) {
        return callback(new Error('证件号码不正确'))
    } else {
        callback()
    }
}

export function checkIsEmpty(rule, value, callback) {
    if (!value && value !== 0) {
        return callback(new Error('不能为空'))
    } else {
        callback()
    }
}
export function checkIsFocusEmpty(rule, value, callback) {
    if (!value && value !== 0) {
        return callback(new Error('不能为空'))
    } else {
        callback()
    }
}

export function checkIsNumber(rule, value, callback) {
    const reg = /^(0|[1-9][0-9]*)$/;
    if (value && !reg.test(value)) {
        return callback(new Error('请填写0或者非0开头的整数'))
    } else {
        callback()
    }
}

export function checkIsRequireNumber(rule, value, callback) {
    const reg = /^(0|[1-9][0-9]*)$/;
    if (!value && value !== 0) {
        return callback(new Error('不能为空'))
    } else if (value && !reg.test(value)) {
        return callback(new Error('请填写0或者非0开头的整数'))
    } else {
        callback()
    }
}

export function checkIsRequireNumberDot(rule, value, callback) {
    const reg = /^(([1-9][0-9]*)|(([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2})))$/;
    if (!value && value !== 0) {
        return callback(new Error('不能为空'))
    } else if (value && !reg.test(value)) {
        return callback(new Error('请填写最多两位小数的数字'))
    } else {
        callback()
    }
}

export function checkIsNumberDot(rule, value, callback) {
    const reg = /^(0|([1-9][0-9]*)|(([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2})))$/;
    if (value && !reg.test(value)) {
        return callback(new Error('请填写最多两位小数的数字'))
    } else {
        callback()
    }
}

export function checkIsNumber100(rule, value, callback) {
    const reg = /^(?:[1-9]?\d|100)$/;
    if (!value && value !== 0) {
        return callback(new Error('不能为空'))
    } else if (value && !reg.test(value)) {
        return callback(new Error('请填写0-100的整数'))
    } else {
        callback()
    }
}
export default {
    //验证手机号码
    checkPhone: [{ required: true, validator: checkPhoneNumber, trigger: ['blur', 'change'] }],
    //验证身份证号码Id
    checkId: [{ required: true, validator: checkIdNum, trigger: ['blur', 'change'] }],
    //验证不能为空
    checkIsEmpty: [{ required: true, validator: checkIsEmpty, trigger: ['blur', 'change'] }],
    //验证不能为空的聚焦状态
    checkIsFocusEmpty: [{ required: true, validator: checkIsEmpty, trigger: ['blur'] }],
    //验证0和不能非0开头的
    checkIsNumber: [{ validator: checkIsNumber, trigger: ['blur', 'change'] }, ],
    //验证必须数字
    checkIsRequireNumber: [{ required: true, validator: checkIsRequireNumber, trigger: ['blur'] }, ],
    //验证必须数字以及小数点
    checkIsRequireNumberDot: [{ required: true, validator: checkIsRequireNumberDot, trigger: ['blur'] }, ],
    //验证0-100的数字
    checkIsNumber100: [{ required: true, validator: checkIsNumber100, trigger: ['blur'] }],
    //验证不必须得最多两位小数的数字
    checkIsNumberDot: [{ validator: checkIsNumberDot, trigger: ['blur', 'change'] }, ],
}